#! /usr/bin/env python
# -*- coding: UTF-8 -*-

from Lib import Exec, Archive, Export, Errors, Remote
import os.path
import re

def backupNow(directories, target_file, type="uncompressed", retention=0, overwrite=0):
    '''
    Backup a directory or a list of directories to a file on
    local filesystem or on a remote host using ssh, samba or ftp
    @param directories: Directory or directories to backup
    Can be a directory, a list of directories or a string
    with space-separated directories
    @param type: Type of compression to use for backup
    Available types are : uncompressed (only tar), gzip, bzip2 or lzma
    @param target_file: Target file for backup
    ++ To use ftp, set file to something like this :
        ftp://user:password@host:port/path_to_file
        -if not set, the connected user is used by default
        -if not set, port 21 is used by default
    ++ To use samba, set file to something like this :
        smb://domain#user:password@host/share/path_to_file
        -if not set, the connected user is used by default
        -if not set, the domain is set to the same value as the host
    ++ To use ssh, set file to something like this :
        ssh://user@host:port/path_to_file
        -if not set, the connected user is used by default
        -if not set, port 22 is used by default
        -ensure that your are able to login to the remote host
        with your public key (~/.ssh/id_dsa.pub or ~/.ssh/id_rsa.pub)    
    '''
    dircheckResult = checkDirectories(directories)
    if dircheckResult[0] != 0:
        return(dircheckResult)
    targetcheckResult = checkTargetFile(target_file)
    if targetcheckResult[0] != 0:
        return(targetcheckResult)
    

def checkDirectories(dirs2check):
    '''
    Verify if every parameters passed to other
    functions are OK.
    @param dirs: directory or directories to backup
    @param file: target file for backup
    '''
    dir_list = []
    if os.path.exists(dirs):
        dir_list.append(dirs)
    elif os.path.exists(dirs[0]):
        for i in directories:
            dir_list.append(i)
    elif os.path.exists(dirs.split()[0]):
        for i in dirs.split():
            dir_list.append(i)
    else:
        return(Errors.Err(99))
    for i in dir_list:
        if not os.path.exists(i):
            return(Errors(90)[0], i + ' ' + Errors.Err(90)[1])
    if os.path.exists(file):
        return(Errors.Err(80))
    return(0, dir_list)

def checkTargetFile(file2check):
    '''
    Check if a file already exists.
    @param file2check: As it sounds like, the file to check.
    '''
    prefix = file2check[0:6]
    if re.search("://", file2check, re.I):
        return(Remote.checkRemoteFile())
    
    return

def TarGzip(directories, target):
    '''
    Make a tar.gz archive from one or more directories
    @param directories : Can be a directory, a list of directories or
    a string with space-separated directories
    @param target : Must be a tar.gz file
    If the file already exists, an incremental counter will be added
    to the name of the file, just before the suffix (eg : file_1.tar.gz)
    If the the file is not a tar.gz file, the suffix tar.gz will be added
    to the name of the file
    '''
    if not Exec.runCmd('tar -zcpf ' + target + ' ' + ' '.join(dir_list))[0]:
        return(1, retstrings[1])
    return(0, retstrings[0])
